//
//  SureModifyPaypwdController.m
//  WaywardPoint_Code
//
//  Created by 韩盼盼 on 15/9/30.
//  Copyright (c) 2015年 易网新科技. All rights reserved.
//

#import "SureModifyPaypwdController.h"
#import "AFFNumericKeyboard.h"
#import "PasswordSetVC.h"//密码重置
#define PayCount 6


@interface SureModifyPaypwdController ()<AFFNumericKeyboardDelegate,HeadBackView>
{
    UIButton *gobackBtn;
    UIView *sureView;
    UILabel *msgLab;
    NSMutableDictionary *userdict;
}
@property(nonatomic,strong)PasswordSetVC *paypwdVc;
@end

@implementation SureModifyPaypwdController
-(PasswordSetVC *)paypwdVc
{
    if (!_paypwdVc) {
        _paypwdVc=[[PasswordSetVC alloc]init];
    }
    return _paypwdVc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.paypwdStr=@"";
    userdict=[NSMutableDictionary dictionary];
    _surepaypwd=[NSMutableArray array];
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    self.view.tag=1;
    HeadBackView *headview=[HeadBackView headbackView:@"确认支付密码" andrightBtn:nil];
    headview.delegate=self;
    [self.view addSubview:headview];
    self.title = @"确认支付密码";

    [self showUI];
    // Do any additional setup after loading the view.
}

- (void)showUI
{
    UIView *contentView=[[UIView alloc]initWithFrame:CGRectMake(0, 65, ScreenW, ScreenH-65)];
    [self.view addSubview:contentView];
    
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake((ScreenW-200)*0.5, 10, 200, 25)];
    title.textColor = COLOT_TEXTGRAY;
    title.font=[UIFont systemFontOfSize:18];
    title.textAlignment=NSTextAlignmentCenter;
    title.text=@"请确认支付密码";
    [contentView addSubview:title];
    
    CGFloat btnH=59;
    
    //支付密码的框
    UIView *pwdview=[[UIView alloc]initWithFrame:CGRectMake(12, title.y+title.height+5, ScreenW-24, 50)];
    CGFloat btnW=pwdview.width/6;
    pwdview.layer.masksToBounds=YES;
    pwdview.layer.borderWidth=0.8;
    pwdview.layer.borderColor = [UIColor whiteColor].CGColor;
    [contentView addSubview:pwdview];
    
    msgLab=[[UILabel alloc]initWithFrame:CGRectMake(pwdview.x, pwdview.y+pwdview.height+5, pwdview.width, 10)];
    msgLab.hidden=YES;
    msgLab.textColor=RXDColor(223, 56, 109);
    msgLab.font=[UIFont boldSystemFontOfSize:12];
    [contentView addSubview:msgLab];
    for (int i=0; i<6; i++) {
        UITextField *pwdla=[[UITextField alloc]initWithFrame:CGRectMake(i*btnW, 0, btnW, btnH)];
        pwdla.textAlignment=NSTextAlignmentCenter;
        pwdla.secureTextEntry=YES;
        pwdla.textColor=[UIColor whiteColor];

        pwdla.enabled=NO;
        pwdla.keyboardType=UIKeyboardTypeNumberPad;
        pwdla.font=[UIFont systemFontOfSize:20];
        pwdla.layer.masksToBounds=YES;
        pwdla.layer.borderWidth=0.8;
        pwdla.layer.borderColor = [UIColor whiteColor].CGColor;

        pwdla.tag=i+300;
        [pwdview addSubview:pwdla];
    }
    
    UIView *keyboardview=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenH-214, ScreenW, 216)];
    [self.view addSubview:keyboardview];
    
    AFFNumericKeyboard *keyboard = [[AFFNumericKeyboard alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 216)];
    [keyboardview addSubview:keyboard];
    keyboard.delegate = self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UITextField *pwdT1=(UITextField *)[self.view viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self.view viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self.view viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self.view viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self.view viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self.view viewWithTag:305];
    pwdT1.text=@"";
    pwdT2.text=@"";
    pwdT3.text=@"";
    pwdT4.text=@"";
    pwdT5.text=@"";
    pwdT6.text=@"";
}

- (void)btnback
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)numberKeyboardBackspace
{
    msgLab.hidden=YES;
    [self clearpaypwd];
}

-(void)numberKeyboardInput:(NSInteger)number
{
    if (self.surepaypwd.count< PayCount) {
        [self.surepaypwd addObject:[NSString stringWithFormat:@"%d",(int)number]];
        [self getpaypwd:self.surepaypwd];
    }
    
}

-(NSArray *)substring:(NSString *)temp
{
    
    NSArray *array = [temp componentsSeparatedByString:@"^"]; //从字符A中分隔成2个元素的数组
    
    return array;
}
- (NSString *)getpaypwd:(NSArray *)pwdstrarr
{
    
    NSString *paypwdstr=@"";
    UITextField *pwdT1=(UITextField *)[self.view viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self.view viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self.view viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self.view viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self.view viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self.view viewWithTag:305];
    
    if (pwdT1.text.length==0) {
        pwdT1.text=pwdstrarr[0];
    }else if (pwdT2.text.length==0)
    {
        pwdT2.text=pwdstrarr[1];
    }else if (pwdT3.text.length==0)
    {
        pwdT3.text=pwdstrarr[2];
    }else if (pwdT4.text.length==0)
    {
        pwdT4.text=pwdstrarr[3];
    }else if (pwdT5.text.length==0)
    {
        pwdT5.text=pwdstrarr[4];
    }else if (pwdT6.text.length==0)
    {
        pwdT6.text=pwdstrarr[5];
    }
    if (pwdT1.text.length!=0&&pwdT2.text.length!=0&&pwdT3.text.length!=0
        &&pwdT4.text.length!=0&&pwdT5.text.length!=0&&pwdT6.text.length!=0) {
        paypwdstr=[NSString stringWithFormat:@"%@%@%@%@%@%@",pwdT1.text,pwdT2.text,pwdT3.text,pwdT4.text,pwdT5.text,pwdT6.text];
        
        if ([self.tempstr isEqualToString:paypwdstr]) {
            _paypwdStr=paypwdstr;
            msgLab.hidden=YES;
            [self settingPaypwdNetwork];
            
        }
        else
        {
            msgLab.hidden=NO;
            msgLab.text=@"两次密码输入不一致";
        }
    }
    
    return  paypwdstr;
}

- (void)clearpaypwd
{
    UITextField *pwdT1=(UITextField *)[self.view viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self.view viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self.view viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self.view viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self.view viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self.view viewWithTag:305];
    if (pwdT6.text.length!=0) {
        pwdT6.text=@"";
        [self.surepaypwd removeLastObject];
    }else if (pwdT5.text.length!=0)
    {
        pwdT5.text=@"";
        [self.surepaypwd removeLastObject];
    }else if (pwdT4.text.length!=0)
    {
        pwdT4.text=@"";
        [self.surepaypwd removeLastObject];
    }else if (pwdT3.text.length!=0)
    {
        pwdT3.text=@"";
        [self.surepaypwd removeLastObject];
    }else if (pwdT2.text.length!=0)
    {
        pwdT2.text=@"";
        [self.surepaypwd removeLastObject];
    }else if (pwdT1.text.length!=0)
    {
        pwdT1.text=@"";
        [self.surepaypwd removeLastObject];
    }
    
}

- (void)getoldpaypwd:(NSString *)oldpwd
{
    _oldpaypwd=oldpwd;
}

-(void)settingPaypwdNetwork
{
    // 增加蒙板
    [MBProgressHUD showMessage:@"密码修改中...."];

    NSString *urlstr=[NSString stringWithFormat:@"%@member/paypassword/update",MRAPI];
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"memberid"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"memberid"];
    params[@"oldpaypassword"] = _oldpaypwd;
    params[@"newpaypassword"] = _paypwdStr;
    
    NSLog(@"c%@",params);
    
    [[HttpRequest shardWebUtil]postNetworkRequestURLString:urlstr andParas:params andTransferGottenData:^(id obj, NSError *error) {
        [MBProgressHUD hideHUD];

        NSLog(@"%@---%@",obj,obj[@"error"]);
        NSString *errorstr = obj[@"error"];
        
        if (![errorstr isEqualToString:@""]) {
            [UIView showAlertMsg:errorstr];
        } else {
            
            [MBProgressHUD showSuccess:@"修改成功"];
            [self popToView];
            
        }

    }];
}

//navigationController在pop时，指定pop到某个页面，这个页面要在navigationController栈里有的
- (void)popToView
{
    for (UIViewController *temp in self.navigationController.viewControllers) {
        
        if ([temp isKindOfClass:[PasswordSetVC class]]) {
            [self.navigationController popToViewController:temp animated:YES];
        }
        
    }
}
@end
